//
//  RescueController.m
//  iCarStyle
//
//  Created by liu nian on 13-9-9.
//  Copyright (c) 2013年 liunian. All rights reserved.
//

#import "RescueController.h"

@implementation RescueItem

@end

@interface RescueController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *datasource;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation RescueController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.title = @"救援电话";
    }
    return self;
}
- (NSString *)tabTitle
{
	return self.title;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = bbi;
    
    [self loadFromDisk];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadFromDisk{
    NSString *pathString = [[NSBundle mainBundle] pathForResource:@"rescue" ofType:@"json"];
    NSData *elementsData = [NSData dataWithContentsOfFile:pathString];
    
    NSError *anError = nil;
    NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData
                                                              options:NSJSONReadingAllowFragments
                                                                error:&anError];
    
    for (NSDictionary *aModuleDict in parsedElements){
        RescueItem *item = [[RescueItem alloc] init];
        item.company = [aModuleDict objectForKey:@"title"];
        item.phonenum = [aModuleDict objectForKey:@"phone"];
        [self.datasource addObject:item];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self tableView];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.datasource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *identifier = @"rescue";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    
    RescueItem *item = [self.datasource objectAtIndex:indexPath.row];
    [cell.textLabel setText:item.company];
    [cell.detailTextLabel setText:item.phonenum];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RescueItem *item = [self.datasource objectAtIndex:indexPath.row];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",item.phonenum]]];
}

#pragma mark
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))
                                                  style:UITableViewStyleGrouped];
        [_tableView setBackgroundColor:[UIColor whiteColor]];
        [_tableView setBackgroundView:nil];
        [_tableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [_tableView setShowsVerticalScrollIndicator:NO];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        _datasource = [[NSMutableArray alloc] initWithCapacity:40];
    }
    return _datasource;
}
@end
